package com.exam.springbootredis.service;
import com.exam.springbootredis.entity.User;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    /**
     * 模拟数据库
     */
    private static final Map<Long, User> DATABASES = Maps.newConcurrentMap();

    /**
     * 初始化数据
     *
     */
    static {
        DATABASES.put(1L,new User(1L,"user1"));
        DATABASES.put(1L,new User(2L,"user2"));
        DATABASES.put(1L,new User(3L,"user3"));
    }
    /**
     *
     * 保存或修改用户
     */
    @CachePut(value = "user",key ="#user.id")
    @Override
    public User saveOrUpdate(User user){
        DATABASES.put(user.getId(),user);
        log.info("保存用户【user】={}",user);
        return user;
    }
    /**
     * 获取用户
     */
    @Cacheable(value = "user", key = "#id")
    @Override
    public User get(Long id) {
        // 我们假设从数据库读取
        log.info("查询用户【id】= {}", id);
        return DATABASES.get(id);
    }
    /**
     * 删除
     */
    @CacheEvict(value = "user", key = "#id")
    @Override
    public void delete(Long id) {
        DATABASES.remove(id);
        log.info("删除用户【id】= {}", id);
    }
}
